package org.sgr.money.manager.frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.sgr.money.manager.biz.CashAcctBiz;
import org.sgr.money.manager.core.meta.AcctTypeEnum;
import org.sgr.money.manager.dto.CashAcctDto;
import org.sgr.money.manager.util.MgrUtil;

public class CashAcctAddComp extends Composite {

	private Group groupAcctType = null;
	private Button radioAccrTypeCash = null;
	private Button radioAccrTypeBank = null;
	private Button radioAccrTypeAr = null;
	private Button radioAccrTypeAp = null;
	private Button radioToDo = null;

	private CLabel cLabAcctName = null;
	private CLabel cLabAcctBal = null;
	private CLabel cLabelAcctMgr = null;
	private Text textAcctName = null;
	private Text textAcctBal = null;
	private Text textAreaAcctMgr = null;
	private Button btnOk = null;
	private Button btnCancel = null;
	private CCombo cComboCurr = null;

	public CashAcctAddComp(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void createAcctBal() {
		cLabAcctBal = new CLabel(this, SWT.NONE);
		cLabAcctBal.setText(CashAcctAddCompMeta.getCLabAcctBalText());
		FormData formDataCLabAcctBal = new FormData();
		formDataCLabAcctBal.top = new FormAttachment(0, 83);
		formDataCLabAcctBal.left = new FormAttachment(0, 10);
		formDataCLabAcctBal.width = 60;
		cLabAcctBal.setLayoutData(formDataCLabAcctBal);

		textAcctBal = new Text(this, SWT.BORDER | SWT.RIGHT);
		FormData formDataTextAcctBal = new FormData();
		formDataTextAcctBal.top = new FormAttachment(0, 83);
		formDataTextAcctBal.left = new FormAttachment(0, 80);
		formDataTextAcctBal.width = 154;
		textAcctBal.setLayoutData(formDataTextAcctBal);
		textAcctBal.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
				int code = e.keyCode;
				if (!MgrUtil.validateNumber(code, textAcctBal.getText(), 2)) {
					e.doit = false;
				}
			}
		});

		cComboCurr = new CCombo(this, SWT.BORDER);
		cComboCurr.add("元");
		cComboCurr.add("美元");
		cComboCurr.setText("元");
		cComboCurr.setEditable(false);
		FormData formDataCComboCurr = new FormData();
		formDataCComboCurr.top = new FormAttachment(0, 83);
		formDataCComboCurr.left = new FormAttachment(0, 249);
		formDataCComboCurr.width = 50;
		cComboCurr.setLayoutData(formDataCComboCurr);
	}

	private void createAcctMgr() {
		cLabelAcctMgr = new CLabel(this, SWT.NONE);
		cLabelAcctMgr.setText("管理方式");
		FormData formDataCLabAcctMgr = new FormData();
		formDataCLabAcctMgr.top = new FormAttachment(0, 113);
		formDataCLabAcctMgr.left = new FormAttachment(0, 10);
		formDataCLabAcctMgr.width = 60;
		cLabelAcctMgr.setLayoutData(formDataCLabAcctMgr);

		textAreaAcctMgr = new Text(this, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		FormData formDataTextAreaAcctMgr = new FormData();
		formDataTextAreaAcctMgr.top = new FormAttachment(0, 113);
		formDataTextAreaAcctMgr.left = new FormAttachment(0, 80);
		formDataTextAreaAcctMgr.width = 194;
		formDataTextAreaAcctMgr.height = 60;
		textAreaAcctMgr.setLayoutData(formDataTextAreaAcctMgr);
	}

	private void createAcctName() {
		cLabAcctName = new CLabel(this, SWT.SHADOW_NONE);
		cLabAcctName.setText("账户名称");
		FormData formDataCLabAcctName = new FormData();
		formDataCLabAcctName.top = new FormAttachment(0, 53);
		formDataCLabAcctName.left = new FormAttachment(0, 10);
		formDataCLabAcctName.width = 60;
		cLabAcctName.setLayoutData(formDataCLabAcctName);

		textAcctName = new Text(this, SWT.BORDER);
		FormData formDataTextAcctName = new FormData();
		formDataTextAcctName.top = new FormAttachment(0, 53);
		formDataTextAcctName.left = new FormAttachment(0, 80);
		formDataTextAcctName.width = 214;
		textAcctName.setLayoutData(formDataTextAcctName);
	}

	private void createButton() {
		btnOk = new Button(this, SWT.NONE);
		btnOk.setText("确定");
		btnCancel = new Button(this, SWT.NONE);
		btnCancel.setText("取消");

		FormData formDataBtnOk = new FormData();
		formDataBtnOk.top = new FormAttachment(0, 190);
		formDataBtnOk.left = new FormAttachment(0, 80);
		formDataBtnOk.width = 90;
		btnOk.setLayoutData(formDataBtnOk);

		FormData formDataBtnCancel = new FormData();
		formDataBtnCancel.top = new FormAttachment(0, 190);
		formDataBtnCancel.left = new FormAttachment(0, 180);
		formDataBtnCancel.width = 90;
		btnCancel.setLayoutData(formDataBtnCancel);

		btnOk.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				CashAcctDto cashAcctDto = createCashAcctDto();
				CashAcctBiz cashAcctBiz = new CashAcctBiz();
				cashAcctBiz.insertCashAcct(cashAcctDto);
				getParent().dispose();
			}
		});

		btnCancel.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				getParent().dispose();
			}
		});
	}

	/**
	 * This method initializes group
	 * 
	 */
	private void createGroup() {
		groupAcctType = new Group(this, SWT.NONE);
		FormData data = new FormData();
		data.top = new FormAttachment(0, 3);
		data.left = new FormAttachment(0, 10);
		groupAcctType.setLayoutData(data);
		RowLayout rowLayout = new RowLayout();
		rowLayout.spacing = 5;
		rowLayout.pack = true;
		groupAcctType.setLayout(rowLayout);
		// group.setp

		groupAcctType.setText("账户类型");
		radioAccrTypeCash = new Button(groupAcctType, SWT.RADIO);
		radioAccrTypeCash.setText("现金");
		radioAccrTypeCash.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
		radioAccrTypeBank = new Button(groupAcctType, SWT.RADIO);
		radioAccrTypeBank.setText("银行");
		radioAccrTypeBank.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
		radioAccrTypeAr = new Button(groupAcctType, SWT.RADIO);
		radioAccrTypeAr.setToolTipText("");
		radioAccrTypeAr.setText("应收款");
		radioAccrTypeAr.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
		radioAccrTypeAp = new Button(groupAcctType, SWT.RADIO);
		radioAccrTypeAp.setText("应付款");
		radioAccrTypeAp.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
		radioToDo = new Button(groupAcctType, SWT.RADIO);
		radioToDo.setText("待处理");
		radioToDo.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
	}

	private void initialize() {
		createGroup();
		createAcctName();
		createAcctBal();
		createAcctMgr();
		createButton();
		setSize(new Point(320, 230));
		setLayout(new FormLayout());
	}

	private CashAcctDto createCashAcctDto() {
		CashAcctDto cashAcctDto = new CashAcctDto();
		cashAcctDto.setAcctName(textAcctName.getText());
		cashAcctDto.setAcctBal(textAcctBal.getText());
		cashAcctDto.setAcctCurr(cComboCurr.getText());
		cashAcctDto.setAcctMgr(textAreaAcctMgr.getText());

		if (radioAccrTypeCash.getSelection()) {
			cashAcctDto.setAcctType(AcctTypeEnum.CASH.getCode());
		} else if (radioAccrTypeBank.getSelection()) {
			cashAcctDto.setAcctType(AcctTypeEnum.BANK.getCode());
		} else if (radioAccrTypeAr.getSelection()) {
			cashAcctDto.setAcctType(AcctTypeEnum.AR.getCode());
		} else if (radioAccrTypeAp.getSelection()) {
			cashAcctDto.setAcctType(AcctTypeEnum.AP.getCode());
		}
		return cashAcctDto;
	}
}
